package com.example.test0702.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.test0702.R;
import com.example.test0702.yan.Phone;

import java.util.List;

public class PhoneAdapter extends RecyclerView.Adapter <PhoneAdapter.Holder>{

    private List<Phone> mPhones;
    private Context mContext;

    public PhoneAdapter(List<Phone> phones,Context context)
    {
        mPhones = phones;
        this.mContext=context;
    }

    @NonNull
    public Holder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.phone_view_layout,parent,false);
        Holder holder=new Holder(view);

        return holder;
    }

    public void onBindViewHolder(@NonNull Holder holder, int position) {
        Phone phone=mPhones.get(position);
        holder.nameTv.setText(phone.getName());
        holder.priceTv.setText(phone.getPrice()+"");
        Glide.with(mContext).load(phone.getImg()).into(holder.phoneIv);
    }

    public int getItemCount() {
        return mPhones.size();
    }

    class Holder extends RecyclerView.ViewHolder{
        TextView nameTv;
        TextView priceTv;
        ImageView phoneIv;

        public Holder(View itemView) {
            super(itemView);
            nameTv=itemView.findViewById(R.id.name_text);
            priceTv=itemView.findViewById(R.id.price_text);
            phoneIv=itemView.findViewById(R.id.phone_iv);
        }
    }

}
